﻿/**
 * PoplyStar
 *
 * @author		Allen Chou
 * @version		1.1.1 (last update: Dec 15 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.display.shapes {
	import flash.events.Event;
	
	import idv.cjcat.display.*;

	/**
	 * The <code>PolyStar</code> class lets you conveniently creates a polygon-star shape,
	 * whose style can be easily set by using <code>LineStyle</code> and <code>Fill</code>.
	 */
	public class PolyStar extends AbstractShape {
		
		/**
		 * The number of sides of the polygon-star.
		 */
		public var sides:uint;
		/**
		 * The radius of the polygon-star.
		 */
		public var radius:Number;
		/**
		 * Whether the polygon-star shape is a star or a polygon.
		 */
		public var star:Boolean;
		/**
		 * The parameter for star shapes. This value should be between one and zero. Smaller values give pointier stars.
		 */
		public var starPointSize:Number;
		
		/**
		 * Creates a polygon-star.
		 * @param	sides          The number of sides of the polygon-star.
		 * @param	radius         The radius of the polygon-star.
		 * @param	star           Whether the polygon-star shape is a star or a polygon.
		 * @param	starPointSize  The parameter for star shapes. This value should be between one and zero. Smaller values give pointier stars.
		 */
		public function PolyStar(sides:uint = 5, radius:Number =50, star:Boolean = false, starPointSize:Number = 0.5) {
			this.sides = sides;
			this.radius = radius;
			this.star = star;
			this.starPointSize = starPointSize;
			render();
		}
		
		/**
		 * Call this method after modifications to properties are made to update appearance.
		 * <p>This method can be used as an event listener.</p>
		 */
		public override function render(e:Event = null):void {
			graphics.clear();
			if ((this.lineStyle != null) || (this.lineStyle.alpha == 0)) CJDisplay.lineStyle(this, lineStyle);
			if ((this.fill != null) || (this.fill.alpha == 0)) CJDisplay.beginFill(this, fill);
			
			var i:Number, theta:Number;
			graphics.moveTo(0, -radius);
			if (!star) {
				for (i = 1; i <= sides; i++) {
					theta = 2 * Math.PI / sides * i;
					graphics.lineTo(radius * Math.sin(theta), -radius * Math.cos(theta));
				}
			} else {
				for (i = 1; i <= sides * 2; i++) {
					theta = 2 * Math.PI / 2 / sides * i;
					if (i % 2 == 0) {
						graphics.lineTo(radius * Math.sin(theta), -radius * Math.cos(theta));
					} else {
						graphics.lineTo(radius * starPointSize * Math.sin(theta), -radius * starPointSize * Math.cos(theta));
					}
				}
			}
		}
		
	}
}